﻿using StudentMocking.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentMocking.Repositories
{
    public class StudentsSampleRepository : IStudentsRepository
    {
        private List<Student> _students;

        public StudentsSampleRepository()
        {
            GetAllStudentInfo();
        }

        private void GetAllStudentInfo()
        {
            _students = new List<Student>()
            {
                new Student {Id = 1, Name = "田",Year=25,Sex = "男" },
                new Student {Id = 2, Name = "清",Year=25,Sex = "女" },
                new Student {Id = 3, Name = "雪",Year=25,Sex = "女" }
            };
        }

        //添加学生
        public Task<Student> AddStudent(Student item)
        {
            item.Id = _students.Select(s => s.Id).Max() + 1;
            _students.Add(item);
            return Task.FromResult(item);

        }
        //删除学生
        public Task<bool> DeleteStudent(int id)
        {
            Student studentDelete = _students.Find(s => s.Id == id);
            if (studentDelete != null)
            {
                return Task.FromResult<bool>(_students.Remove(studentDelete));
            }
            return Task.FromResult<bool>(false);
        }

        //根据id查找学生
        public Task<Student> GetAllAnsyc(int id)
        {
            return Task.FromResult(_students.Find(s => s.Id == id));
        }

        //获取所有学生
        public Task<IEnumerable<Student>> GetStudentsAnsyc()
        {
            return Task.FromResult<IEnumerable<Student>>(_students);
        }

        //修改学生
        public Task<Student> UpdateStudent(Student item)
        {
            Student studentUpdate = _students.Find(s => s.Id == item.Id);
            if (studentUpdate != null)
                return null;
            int info = _students.IndexOf(studentUpdate);
            _students[info] = item;
            return Task.FromResult(_students[info]);

        }
    }
}
